package net.zjitc.dao;

import net.zjitc.utils.db.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/12/13.
 */
public class TransationTest {
    //事务操作数据库
    public static void main(String[] args) {
        Connection conn  = null;
        QueryRunner runner = new QueryRunner();
        try{
            conn = C3P0Utils.getConnection();
            conn.setAutoCommit(false);
            conn.setSavepoint();
            String sql1 = "UpDAte account set money = money-100 where uname = 'a'";
            String sql2 = "UpDAte account set money = money+100 where uname = 'b'";
            runner.update(conn,sql1);

            int count = runner.update(conn,sql2);
            if(count==0){
                conn.rollback();
            }
            conn.commit();
            conn.close();

        } catch (SQLException e) {
            try{
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }
}
